package com.shaoyu.elasticsearch.service;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.shaoyu.elasticsearch.util.IpConvert;
import com.shaoyu.elasticsearch.util.PropertiesUtil;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class ElasticSearchClientService {

    private static final TransportClient client = ClientFactory.getTransportClientClient();

    public static Client getNormalTransportClient(){
        return client;
    }

    public static TransportClient getConfigClient(String clusterName) {
        if(clusterName==null){
            clusterName = ElasticSearchClientService.clusterName;
        }
        return  ClientFactory.getTransportClientClient(clusterName,clientTransportSniff,ignoreClusterName,pingTimeout,nodesSamplerInterval);
    }
    public static void close(Client client){
        if(client!=null){
            try{
                client.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    /**集群名称*/
    private static String clusterName = PropertiesUtil.getValue("cluster.name","");

    /**自动嗅探配置 true/false */
    private static String clientTransportSniff = PropertiesUtil.getValue("client.transport.sniff","false");

    /**client.transport.ignore_cluster_name  //设置 true ，忽略连接节点集群名验证*/
    private static String ignoreClusterName = PropertiesUtil.getValue("client.transport.ignore_cluster_name","true");

    /**client.transport.ping_timeout       //ping一个节点的响应时间 默认5秒*/
    private static String pingTimeout = PropertiesUtil.getValue("client.transport.ping_timeout","5s");

    /**client.transport.nodes_sampler_interval //sample/ping 节点的时间间隔，默认是5s*/
    private static String nodesSamplerInterval = PropertiesUtil.getValue("client.transport.ping_timeout","5s");


    public String getClusterName() {
        return clusterName;
    }

    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }

    public String getClientTransportSniff() {
        return clientTransportSniff;
    }

    public void setClientTransportSniff(String clientTransportSniff) {
        this.clientTransportSniff = clientTransportSniff;
    }

    public String getIgnoreClusterName() {
        return ignoreClusterName;
    }

    public void setIgnoreClusterName(String ignoreClusterName) {
        this.ignoreClusterName = ignoreClusterName;
    }

    public String getPingTimeout() {
        return pingTimeout;
    }

    public void setPingTimeout(String pingTimeout) {
        this.pingTimeout = pingTimeout;
    }

    public String getNodesSamplerInterval() {
        return nodesSamplerInterval;
    }

    public void setNodesSamplerInterval(String nodesSamplerInterval) {
        this.nodesSamplerInterval = nodesSamplerInterval;
    }

    private static class ClientFactory{

        private ClientFactory(){}

        public static TransportClient client = null;


        public static TransportClient getTransportClientClient() {
            if(client==null){
                try {
                    client = new PreBuiltTransportClient(Settings.builder().build())
                            .addTransportAddresses(
                                    new InetSocketTransportAddress(
                                            InetAddress.getByName("127.0.0.1")
                                            , Integer.parseInt(PropertiesUtil.getValue("client.port","9300"))));

                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }
            return client;
        }

        public static TransportClient getTransportClientClient(String clusterName, String clientTransportSniff, String ignoreClusterName, String pingTimeout, String nodesSamplerInterval) {
            TransportClient transportClient = null;
            try{
                Settings.Builder build = Settings.builder();
                if (StringUtils.isNotEmpty(clusterName)){
                    build.put("cluster.name",clusterName);
                }
                build.put("client.transport.sniff",Boolean.parseBoolean(clientTransportSniff));
                build.put("client.transport.ignore_cluster_name",Boolean.parseBoolean(ignoreClusterName));
                build.put("client.transport.ping_timeout",pingTimeout);
                build.put("client.transport.nodes_sampler_interval",nodesSamplerInterval);
                transportClient = new PreBuiltTransportClient(build.build());
                transportClient.addTransportAddress(
                        new InetSocketTransportAddress(
                                InetAddress.getByName(new IpConvert().convert(null))
                                , Integer.parseInt(PropertiesUtil.getValue("client.port","9300"))));
            }catch (Exception e){
                e.printStackTrace();
            }
            return transportClient;
        }
    }
}
